﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EggHarvest.GraphicsSupport;
using Microsoft.Xna.Framework;

namespace EggHarvest
{
    class MyGame
    {
        //Bag
        private TexturedPrimitive mBag;
        Vector2 bagSize = new Vector2(10,10);
        private Vector2 bagPosition = new Vector2(50,25);

        //Egg
        private List<Egg> mEggList;
        private TimeSpan mCreationTimeStamp;
        private int mTotalEgg = 0;
        private const int kEggSecInterval = 1000;

        //Game state
        private int score = 0;
        private int eggMissed = 0;
        private const int eggTouchScore = 1;
        
        private TexturedPrimitive mFinal = null;

        public MyGame()
        {
            //Bag
            mBag = new TexturedPrimitive("Ro",bagPosition,bagSize);

            //Egg
            mCreationTimeStamp = new TimeSpan(0);
            mEggList = new List<Egg>();
        }

        public void UpdateGame(GameTime gameTime)
        {
            #region step a
            if(null != mFinal)
                return;
            #endregion

            mBag.Update(InputWrapper.ThumbSticks.Right);

            #region handle collision

            for (int b = mEggList.Count - 1; b >= 0; b--)
            {
                if (mBag.PrimitivesTouches(mEggList[b]))
                {
                    mEggList.RemoveAt(b);
                    score++;
                }
            }

            #endregion

            #region create new egg

            TimeSpan timePassed = gameTime.TotalGameTime;
            timePassed = timePassed.Subtract(mCreationTimeStamp);
            if (timePassed.TotalMilliseconds>kEggSecInterval)
            {
                mCreationTimeStamp = gameTime.TotalGameTime;
                Egg egg = new Egg();
                mTotalEgg++;
                mEggList.Add(egg);
            }
            #endregion

           
        }

        public void DrawGame()
        {
            mBag.Draw();
            foreach (var egg in mEggList)
            {
                egg.Draw();
            }

            if (null != mFinal)
            {
                mFinal.Draw();
            }

            FontSupport.PrintStatusAt(new Vector2(50f,80f), "Status: "+"Score: " + score + " Egg: Generated("+mTotalEgg+")",null);
        }
    }
}
